//
//  ModuleInfo.m
//  ModuleTesting
//
//  Created by vo loc on 8/3/11.
//  Copyright 2011 Epsilon Mobile. All rights reserved.
//

#import "ModuleInfo.h"


@implementation ModuleInfo
@synthesize dict = _dict;

+ (ModuleInfo*)moduleInfoWithName:(NSString*)moduleName
{
  NSString* modulePlistFilePath = [[NSBundle mainBundle] pathForResource:moduleName ofType:@"plist"];
  NSDictionary* dict = [NSDictionary dictionaryWithContentsOfFile:modulePlistFilePath];
  if( dict )
    return [ModuleInfo moduleInfoWithDict:(NSDictionary*)dict];
  
  return nil;
}

+ (ModuleInfo*)moduleInfoWithDict:(NSDictionary *)dict
{
	return [[[ModuleInfo alloc] initWithDict:dict] autorelease];
}

- (id)initWithDict:(NSDictionary*)dict
{
	self = [super init];
  if( self )
  {
  	_dict = [dict retain];
  }	
  return self;
}

- (void)dealloc
{
  [_dict release];
  
	[super dealloc];
}

#pragma mark Accessor
- (NSString*)name
{
 	return [_dict objectForKey:kKeyModuleName]; 
}
- (NSString*)description
{
	return [_dict objectForKey:kKeyModuleDescription];
}

- (BOOL)isClassExists:(NSString*)className
{
  return ( objc_getClass([className UTF8String]) != Nil );
}

- (BOOL)hasTesters
{
	return ([self numberOfDemos] + [self numberOfTestCase]) > 0;
}

- (BOOL)isGTMTestCase:(NSString*)classname
{
  Class class = objc_getClass([classname UTF8String]);
  if( class == Nil )
    return FALSE;
  
  Class superClass = class_getSuperclass(class);
  Class gtmTestClass = objc_getClass("GTMTestCase");
  
  return [superClass isEqual:gtmTestClass];
}

- (BOOL)isViewControllerTester:(NSString*)classname
{
  Class class = objc_getClass([classname UTF8String]);
  if( class == Nil )
    return FALSE;
  
  Class superClass = class_getSuperclass(class);
  Class vcTestClass = objc_getClass("UIViewController");
  
  return [superClass isEqual:vcTestClass];  
}

- (int)numberOfTestCase
{
	int count = 0;
  if( [_dict objectForKey:kKeyMainTesterClassName] != nil )
  {
    if( [self isGTMTestCase:[_dict objectForKey:kKeyMainTesterClassName]] )
    	count++;
  }
  
  if( [_dict objectForKey:kKeyVariantTestClassArray] )
  {
   	NSArray* arr = [_dict objectForKey:kKeyVariantTestClassArray];
    for( NSString* tester in arr )
    {
    	if( [self isGTMTestCase:tester] )
        count++;
    }
  }
  
  return count;
}
- (int)numberOfDemos
{
	int count = 0;
  if( [_dict objectForKey:kKeyMainTesterClassName] != nil )
  {
    if( [self isViewControllerTester:[_dict objectForKey:kKeyMainTesterClassName]] )
    	count++;
  }
  
  if( [_dict objectForKey:kKeyVariantTestClassArray] )
  {
   	NSArray* arr = [_dict objectForKey:kKeyVariantTestClassArray];
    for( NSString* tester in arr )
    {
    	if( [self isViewControllerTester:tester] )
        count++;
    }
  }
  
  return count;
}

- (NSArray*)allTestsuits
{
	NSMutableArray* testSuites = [[NSMutableArray alloc] init];
  
  if( [_dict objectForKey:kKeyMainTesterClassName] != nil )
  {
    if( [self isGTMTestCase:[_dict objectForKey:kKeyMainTesterClassName]] )
    	[testSuites addObject:[_dict objectForKey:kKeyMainTesterClassName]];
  }
  
  if( [_dict objectForKey:kKeyVariantTestClassArray] )
  {
   	NSArray* arr = [_dict objectForKey:kKeyVariantTestClassArray];
    for( NSString* tester in arr )
    {
    	if( [self isGTMTestCase:tester] )
        [testSuites addObject:tester];
    }
  }
  
  return [testSuites autorelease];
}

- (NSArray*)allDemos
{
 	NSMutableArray* testSuites = [[NSMutableArray alloc] init];
  
  if( [_dict objectForKey:kKeyMainTesterClassName] != nil )
  {
    if( [self isViewControllerTester:[_dict objectForKey:kKeyMainTesterClassName]] )
    	[testSuites addObject:[_dict objectForKey:kKeyMainTesterClassName]];
  }
  
  if( [_dict objectForKey:kKeyVariantTestClassArray] )
  {
   	NSArray* arr = [_dict objectForKey:kKeyVariantTestClassArray];
    for( NSString* tester in arr )
    {
    	if( [self isViewControllerTester:tester] )
        [testSuites addObject:tester];
    }
  }
  
  return [testSuites autorelease]; 
}
@end
